$(function(){

  
 getUserInfo()
 getUser()
//  页面渲染函数
function getUser(){
  $('.userimg img').attr('src',nowuser.profilepic)
  $('.username').html(nowuser.username)
  if(nowuser.sex =='女'){
    $('.usexSexTag').removeClass('icon-nan')
    $('.usexSexTag').addClass('icon-nv')
    $('.usexSexTag').css('color','#ff8096')
  }
  if(nowuser.vipStatus==1){
    $('.uservipTag').css('color','#f2ff03')
  }
}


// 样式覆盖
  // 隐藏导航栏显示 ，直接显示，不在依赖滚动显示
  $(function(){
    $(window).on('scroll',function(){
      const n = document.documentElement.scrollTop;
      if(n>= 170){
        $('.mikufun_header_hide').css('opacity','1')
      }
      if(n<= 170){
        $('.mikufun_header_hide').css('opacity','1')
      }
    })
  
  // 基础样式
  // nav栏切换
  $('.selectinfo>span').on('click',function(){
    $('.'+$('.spanactive')[0].id).css('display','none')
    $('.spanactive').removeClass('spanactive')
    $(this).addClass('spanactive')
    $('.'+$(this)[0].id).css('display','block')
  })
  
  // 我的视频管理发布视频按钮跳转
  //逻辑错误
  // $('.topvoide').on('click',function(){
    // $('.'+$('.spanactive')[0].id).css('display','none')
    // $('.spanactive').removeClass('spanactive')
    // $(this).addClass('spanactive')
    // $('.'+$(this)[0].id).css('display','block')
  // })
  
  //上传图片/视频预览
  $('.upvoideimg').on('change',function(){
    var blob = window.URL.createObjectURL( $('.upvoideimg')[0].files[0])
    $('.imgCanvas').css("backgroundImage","url("+blob+")")
    $('.imgbutton>span').html("点击切换图片")
  })
  //视频
  $('.upvoidebox .videobutton .upvoide').on('change',function(e){
    const fileReader = new FileReader()
    fileReader.readAsDataURL(e.target.files[0])
    fileReader.onload = (e) => {
    $('.voideCanvas video')[0].src = e.target.result
   }
    $('.videobutton>span').html("点击切换视频")
  })
  
  //发布视频按钮
  $('.uploadvoide').on('click',upload)
  
  // 视频发布函数，表单数据上传
  function upload(){
    const formData = new FormData()
    const videoTitle = $('.title').val()
    const videoIntro = $('.intro').val()
    const videoImg = $('.upvoideimg')[0].files[0]
    const video = $('.upvoide')[0].files[0]
    const userId = nowuser.uid
    if(!videoTitle || !videoIntro || !videoImg || !video){
      alert('内容不能为空！')
      return
    }
    formData.append('videoTitle',videoTitle)
    formData.append('videoIntro',videoIntro)
    formData.append("videoImg",videoImg)
    formData.append("video",video)
    formData.append('userId',userId)
    $.ajax({
      url:'upload',
      type:'POST',
      cache:false,
      processData: false ,
      contentType: false ,
      dataType:"json",
      data:formData,
      success:function(res){
        $('.upvoideform')[0].reset()
        $('.imgCanvas').css('backgroundImage','url("../img/mikufun/icon_add_img.png")')
        $('.upvoidebox .voideCanvas video')[0].src =" "
        resSuccess('上传成功')
      },
      error:function(error){
        $('.upvoideform')[0].reset()
        $('.imgCanvas').css('backgroundImage','url("../img/mikufun/icon_add_img.png")')
        $('.upvoidebox video')[0].src =" "
        alert('文件上传失败')
      }
    })
  
    // axios({
    //   url: 'upload',
    //   method: 'POST',
    //   headers: {
    //     "Content-Type":"multipart/form-data",
    //   },
    //   data: formData
    // }).then(res =>{
    //   console.log(res)
    //   $('.upvoideform')[0].reset()
    //   console.log(111111)
    // }).catch(error =>{
    //   alert('文件上传失败')
    //   $('.upvoideform')[0].reset()
    //   console.log(2222222)
    // })
  }
  
    //我的收藏
    axios({
      url:'collections2',
      method:'GET',
      params:{
        uid:nowuser.uid,
        page:1
      }
    }).then(function(res){
      console.log(res)
      $(res.data.data).each(function (index, ele) {
        $('.myvoideInfo .guess').append(`
            <div class="guessbox">
            <a href="./animedetail.html?vid=${ele.videoId}" target="_blank" rel="opener">
            <img src="${ele.videoImg}" alt="">
            <p>${ele.videoTitle}</p>
            <span>${ele.upTime}</span>
            </a>
          </div>`)
      })
  
    })
  })
  
  // 获取用户信息，设置用户信息,动态
  function getUserInfo(){
    axios({
      url:'getUserInfo/'+nowuser.uid,
      method:'GET',
      params:{
        uid:nowuser.uid
      }
    }).then(res=>{
      console.log(res)
      let data = res.data.data
      if(data.length==0){
        return
      }
      let list =''
      for(let i=0;i<data.length;i++){
        let time = data[i].video.videoTime.split(" ")[0]
        list +=
          ` <div class="historybox">
              <span class="time">发布时间：${time}</span>
               <a href="./uservideodetail.html?vid=${data[i].video.videoId}">
                <img src="${data[i].detail.detailImg}" alt="${data[i].video.videoTitle}">
              </a>
              <p>${data[i].video.videoTitle}</p>
              <div class="historymsg">
                <span>${data[i].username}</span>
                <span></span>
              </div>
              <img class="delbtn" src="./img/upload/删除.svg" data-id=${data[i].video.videoId} alt="">
            </div>
          `
      }
      $('.myvoideInfo .history').append(list)
    })
  }


  // 删除动态视频
  $('.history').on('click','.delbtn',function(e){
    if(confirm('确定要删除吗?')){
      let videoId = $(e.target).attr('data-id')
      console.log(videoId)
      axios({
        url:'deletedetail/'+videoId,
        method:'DELETE',
        params:{
          videoId:videoId
        }
      }).then(res=>{
        if(res.data.code==210){
          $('.myvoideInfo .history').empty()
          resSuccess('删除成功')
          getUserInfo()
        }else{
          resError('删除失败')
        }
      }).catch(error=>{
        resError('出错了')
      })
    }
  })

  //搜索动态视频
  
  $('.serachbtnMV').on('click',function(){
    let userId =nowuser.uid
    let videoTitle =$('.userVoideSerach input').val()
    $('.userVoideSerach input').val('')
    axios({
      url:'getMyVideo',
      method:'GET',
      params:{
        userId:userId,
        videoTitle:videoTitle
      }
    }).then(res=>{
      console.log(res)
      let data = res.data.data
      if(data.length==0){
        $('.myvoideInfo .history').empty()
        return
      }
      let list =''
      for(let i=0;i<data.length;i++){
        let time = data[i].videoTime.split(" ")[0]
        list +=
          ` <div class="historybox">
              <span class="time">发布时间：${time}</span>
               <a href="./uservideodetail.html?vid=${data[i].videoId}">
                <img src="${data[i].detail.detailImg}" alt="${data[i].videoTitle}">
              </a>
              <p>${data[i].videoTitle}</p>
              <div class="historymsg">
                <span>${data[i].user.username}</span>
              </div>
              <img class="delbtn" src="./img/upload/删除.svg" data-id=${data[i].videoId} alt="">
            </div>
          `
      }
      $('.myvoideInfo .history').empty()
      $('.myvoideInfo .history').append(list)
    })
  })

  //显示全部
  $('.topvoide').on('click',function(){
    $('.myvoideInfo .history').empty()
    getUserInfo()
  })

//更改用户信息




})

 